 
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>平台用户</title>
	<link rel="stylesheet" type="text/css" href="../../rs/plugins/easyui/themes/metro/easyui.css">
	<link rel="stylesheet" type="text/css" href="../../rs/plugins/easyui/themes/icon.css">
	<link href="../../rs/css/bootstrap.min.css" rel="stylesheet">
	<script type="text/javascript" src="../../rs/plugins/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../../rs/plugins/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../rs/plugins/easyui/locale/easyui-lang-zh_CN.js"></script>
	<script src="../../rs/js/jquery.cookie.js"></script>
	<script src="../../rs/js/common_qj.js"></script>
	<style>
		html,body{ margin:0px; height:100%;}
	</style>
	<?php
      require_once('login.php');
     ?>   
</head>
<body >
<table id="table" class="easyui-datagrid"  style="width:100%;height:100%;padding: 10px;"
			data-options="
			rownumbers:true,
			singleSelect:'true',
			method:'get' ,
			toolbar:'#easyui_toolbar',
			fit:true,
			fitColumns:true">
		<thead>
			<tr>
				<th data-options="field:'ck',checkbox:true"></th>
				<th data-options="field:'bh',align:'center',formatter:formatId,width:20">用户编号</th>
				<th data-options="field:'mc',align:'center',width:30">用户名称</th>
				<th data-options="field:'sj',align:'center',width:30">用户手机</th>
				<th data-options="field:'grmc',align:'center',width:30">权限组</th>
			</tr>
		</thead>
	</table>

	<div id="easyui_toolbar" region="north" border="false"
	  style="height: 45px;padding: 5px 5px; ">
		<div style="float: left;">
			<button id="shanchu" style="float:left; margin-right:5px; border-radius: 0px;height:34px "  class="btn " onclick="data_remove()" >
				<i class="glyphicon glyphicon-trash"></i>
			</button>
		</div>
		<div style="float: left;">
			<button id="xinzeng" style="float:left;margin-left:5px;  border-radius: 0px;" class="btn " onclick="open_modal()"  >
				<i class="glyphicon glyphicon-plus"></i> 新增
			</button>
		</div>
		<div id="tb" class="input-group" style="float: right; margin-right: 10px;width:350px">
			<input type="text" class="form-control" id="searchtext" placeholder="输入搜索关键字" value="">
			<div class="input-group-btn">
				<button type="button" class="btn dropdown-toggle" onclick="do_search()" style="height:34px;border-radius: 0px;">
					搜索
				</button>
			</div>
		</div>
	</div>


<div style="display: none" id="open_edit">
	<div id="edit_from">
		<div class="input-group" style="display:none">
			<div class="input-group-btn">
				<button type="button" class="btn   dropdown-toggle " data-toggle="dropdown"> 企业编号</button>
			</div>
			<input type="text" class="form-control "   name="rid"    required="true"  >
		</div>
		<div class="input-group" style="padding: 20px 20px 10px 20px;">
			<div class="input-group-btn">
				<button type="button" class="btn dropdown-toggle " data-toggle="dropdown">用户编号</button>
			</div>
			<input type="text" class="form-control "   name="bh"    required="true"  >
		</div>
		<div class="input-group" style="padding: 0px 20px 10px 20px;">
			<div class="input-group-btn">
				<button type="button" class="btn   dropdown-toggle " data-toggle="dropdown">用户名称</button>
			</div>
			<input type="text" class="form-control "   name="mc">
		</div>
		<div class="input-group" style="padding: 0px 20px 10px 20px;">
			<div class="input-group-btn">
				<button type="button" class="btn   dropdown-toggle " data-toggle="dropdown">用户手机</button>
			</div>
			<input type="text" class="form-control " name="sj">
		</div>
    <div class="input-group" style="padding: 0px 20px 10px 20px;">
			<div class="input-group-btn">
				<button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">&nbsp;&nbsp;&nbsp;&nbsp;权限组</button>
			</div>
			<!--<input type="text" class="form-control"  name="GroupName">-->
			<select class="form-control" id="qxz"></select>
		</div>
		<div class="input-group" style="padding: 0px 20px 10px 20px;">
			<div class="input-group-btn">
				<button style="margin-right: -2px" type="button" class="btn   dropdown-toggle " data-toggle="dropdown">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</button>
			</div>
			<input type="password" class="form-control " id="mm"  name="mm">
		</div>
		<div class="lay_foot">
			<button type="reset" id="reset" style="display:none;">reset</button>
			<button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"
					style="background-color: #e2e2e2;" onclick="layer.closeAll()">返回
			</button>
			<button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"
					style="background-color: #f54141;color: white;float: right" id="save_do">保存
			</button>
		</div>
	</div>
</div>

<!--页面加载缓冲图标-->
<div id='loadingDiv' style="position:absolute;z-index:1000;top:0px; left:0px;
	width:100%;height:100%;background:white;text-align: center;">
	<h1 style="top: 30%; position: relative;">
		<img src="../../rs/img/loading-2.gif" /><br>
	</h1>
</div>
<script>

	/*页面加载缓冲*/
	function closeLoading() {
		$("#loadingDiv").fadeOut("normal", function () {
			$(this).remove();
		});
	}
	var no;
	$.parser.onComplete = function () {
		if (no) clearTimeout(no);
		no = setTimeout(closeLoading, 1000);
	}
	var sign = URLencode($.cookie('sign'));

	$(function(){

		if(sign==="undefined"){
			window.location.href = "login.html";
		}
		f_sheard();
		var getdata;
    get_qxz();
    sqx();





	});
	//判断用户权限&加载数据
	function f_sheard(){
		$.ajax({
			type: "post",
			url:G_url+'syus/see',
			data: 'sign=' + sign,
			dataType: "json",
			success: function (usedata){
				getdata=usedata;
				if (usedata.ok) {
					$('#table').datagrid({
						data: usedata.data
					});
				}else{
					window.location.href = 'login.html';
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}
	$("#save_do").on("click",function(){
		var layer_title=$('.layui-layer-title').text();
    g_layer_title=layer_title;
		var rid=$("input[name='rid']").val();
		var bh=$.trim($("input[name='bh']").val());
		var mc=$.trim($("input[name='mc']").val());
		var sj=$.trim($("input[name='sj']").val());
   	    var qxz=$.trim($('#qxz').find("option:selected").val());
        var qxz_name=$.trim($('#qxz').find("option:selected").text());
		var mm=$.trim($("input[name='mm']").val());
		if(mm.length!==32){
			mm=$.md5(mm);
		};
		if(layer_title=="新增"){
			 $('#edit_form input').val("");
			if (!f_valid()) return;
			$.ajax({
				type: "post",
				url:G_url+'doi_bh/add/syus'+'?sign='+sign,
				data:"bh="+bh+"&mc="+mc+"&sj="+sj+"&mm="+mm+"&sygr_id="+qxz,
				dataType: "json",
				success: function (result){
					if (result.ok){
						var dd=result.user;
						$('#table').datagrid('appendRow',dd);
						layer.msg(result.msg);
					} else {
						layer.msg(result.msg);
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				}
			});
			layer.closeAll();

		}else if(layer_title=="修改"){
			$.ajax({
				type: "post",
				url:G_url+'doi_bh/edit/syus'+'?'+'sign='+sign,
				data:"rid="+rid+"&bh="+bh+"&mc="+mc+"&sj="+sj+"&mm="+mm+"&sygr_id="+qxz,
				dataType: "json",
				success: function (result){
					if (result.ok){
            	f_sheard();
						layer.msg(result.msg);
					} else {
						layer.msg(result.msg);
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				}
			});
			layer.closeAll();
		}
	});
		/*删除操作*/
	function data_remove(){
		var rows = $('#table').datagrid('getSelections');
		if(rows.length!=0){
			layer.msg('确定删除吗？', {
				time:0,
				btn: ['确定', '取消'] //按钮
				,yes: function do_remove() {
				var bhs = $.map(rows, function (row) {
					return row.bh
				});
				$.ajax({
					type: "post",
					url: G_url+'doi_bh/del/syus'+'?'+'sign='+sign,
					data:"bh=" + bhs,
					dataType: "json",
					success: function (result) {
						if (result.ok) {
							layer.closeAll();
							f_sheard();
							layer.msg(result.msg);

						} else {
							layer.msg(result.msg);
						}
					},
					error: function (XMLHttpRequest, textStatus, errorThrown) {
						//alert(errorThrown);
					}
				});
			}
			}
			);
		}else{
			layer.msg("请先选择！");
		}
	}
	//删除操作



	// 产品搜索操作
	$('#searchtext').bind('keypress',function(event){
		if(event.keyCode == "13") {
			do_search();
		}
	});
	function do_search(){
		var gjc=$('#searchtext').val(); //搜索关键词
		if(gjc==""){
			$('#table').datagrid({
				data: getdata.data
			});
		}else{
			var newdata=[];
			for(var i=0;i<getdata.data.length;i++){
				str=transform(getdata.data[i]).join(',');
				row=str.indexOf(gjc);
				if(row>=0){
					newdata.push(getdata.data[i]);
				}
			}
			$('#table').datagrid({
				data: newdata
			});
		}
	}
	function transform(obj){
		var arr = [];
		for(var item in obj){
			arr.push(obj[item]);
		}
		return arr;
	}


		function open_modal(){
      sqx_save("新增");
			$("#open_edit").show();
			$('#reset').click();
			layer.open({
				title: "新增",
				type: 1,
        //offset: '10px',//top顶部偏移
        offset: 'rb',
        shift: 'left',
        //
        //
        area: ['40%', '100%'], //宽高

				shadeClose: true,
				content: $('#edit_from')
			});
		}

		function open_edit(r){
			
     	    sqx_save("修改");
			var rowdata=$('#table').datagrid('getData');
			
			var row=rowdata.rows[r];
			console.log(row);
			var qxz_mc=row.grmc;
			 
			$("#qxz").find("option[value="+qxz_mc+"]").prop("selected",true);
			if (row){
				$("#open_edit").show();
				$('#edit_from').form('load',row);
				 layer.open({
				 title: "修改",
				 type: 1,
         		 offset: 'rb',
         		 area: ['40%', '100%'], //宽高
  				 shadeClose: true,
				 content: $('#edit_from')
				});
			}
		}
		function formatId(value,rowData,rowIndex){
			return "<a href='javascript:void(0)' onclick='open_edit("+rowIndex+");'>"+value+"</a>";
		}


	var f_valid = function(){
		var bh = $.trim($("input[name='bh']").val()) ;
		if( bh =='') {
			layer.msg('请输入用户编号');
			return false;
		}
		var mc = $.trim($("input[name='mc']").val()) ;
		if( mc =='') {
			layer.msg('请输入用户名称');
			return false;
		}
		var sj = $.trim($("input[name='sj']").val()) ;
		if( sj =='') {
			layer.msg('请输入用户手机');
			return false;
		}
		var mm = $.trim($("input[name='mm']").val()) ;
		if( mm =='') {
			layer.msg('请输入密码');
			return false;
		}
		return true;
	};


  //获取权限组
  function get_qxz(){
    $.ajax({
      type: "post",
      url:G_url+'doi/see/sygr',
      data:'sign='+sign,
      dataType: "json",
      success: function (result){
        //给门店编号做循环
        var ff = result.data;
        var html = '';
        $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
          html += "<option value='"+b.mc+"'>" + b.mc + "</option>";//拼接字符串
        });
        $("#qxz").append(html);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
      }
    });
  }
</script>

<script src="../../rs/plugins/layer/layer.min.js"></script>
<script src="../../rs/js/bootstrap.js"></script>
<script src="../../rs/plugins/md5.js"></script>
</body>
</html>
